/*
 * Copyright 2011 Daniel Krieg
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.code.csf;

import com.google.code.csf.locators.ServiceHolder;
import com.google.code.csf.locators.ServiceLocatorChain;
import com.google.code.csf.locators.ejb.LocalSlsbServiceLocator;
import com.google.code.csf.locators.ejb.RemoteSlsbServiceLocator;
import com.google.code.csf.locators.jaxrpc.JBossJaxRpcServiceFactoryServiceLocator;
import com.google.code.csf.locators.jaxrpc.JaxRpcServiceFactoryServiceLocator;
import com.google.code.csf.locators.jaxrpc.JaxRpcServiceRefServiceLocator;
import com.google.code.csf.locators.jaxws.JaxWsServiceRefServiceLocator;
import com.google.code.csf.locators.jaxws.JaxWsServiceServiceLocator;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class ServiceTemplateBuilder<ServiceType> {
    private List<ServiceLocator<ServiceType>> serviceLocators = new ArrayList<ServiceLocator<ServiceType>>();
    private final Class<ServiceType> serviceTypeClass;

    public static <ServiceType> ServiceTemplateBuilder<ServiceType> templateFor(Class<ServiceType> serviceTypeClass) {
        return new ServiceTemplateBuilder<ServiceType>(serviceTypeClass);
    }

    private ServiceTemplateBuilder(Class<ServiceType> serviceTypeClass) {
        this.serviceTypeClass = serviceTypeClass;
    }

    public ServiceTemplateBuilder<ServiceType> withJBossJaxRpcServiceFactoryServiceLocator(URL wsdlDocumentUrl, URL jaxrpcMappingDocumentUrl, Class portInterface, String endpointAddress) {
        JBossJaxRpcServiceFactoryServiceLocator<ServiceType> locator = new JBossJaxRpcServiceFactoryServiceLocator<ServiceType>();
        locator.setJaxrpcMappingDocumentUrl(jaxrpcMappingDocumentUrl);
        return withJaxRpcServiceFactoryServiceLocator(locator, wsdlDocumentUrl, portInterface, endpointAddress);
    }

    public ServiceTemplateBuilder<ServiceType> withJaxRpcServiceFactoryServiceLocator(URL wsdlDocumentUrl, Class portInterface, String endpointAddress) {
        return withJaxRpcServiceFactoryServiceLocator((Class) null, wsdlDocumentUrl, portInterface, endpointAddress);
    }

    public ServiceTemplateBuilder<ServiceType> withJaxRpcServiceFactoryServiceLocator(Class serviceFactoryClass, URL wsdlDocumentUrl, Class portInterface, String endpointAddress) {
        JaxRpcServiceFactoryServiceLocator<ServiceType> locator = new JaxRpcServiceFactoryServiceLocator<ServiceType>();
        locator.setServiceFactoryClass(serviceFactoryClass);
        return withJaxRpcServiceFactoryServiceLocator(locator, wsdlDocumentUrl, portInterface, endpointAddress);
    }

    public ServiceTemplateBuilder<ServiceType> withJaxRpcServiceRefServiceLocator(Class portInterface, String endpointAddress) {
        JaxRpcServiceRefServiceLocator<ServiceType> locator = new JaxRpcServiceRefServiceLocator<ServiceType>();
        locator.setPortInterface(portInterface);
        locator.setEndpointAddress(endpointAddress);
        return withServiceLocator(locator);
    }

    public ServiceTemplateBuilder<ServiceType> withJaxWsServiceServiceLocator(URL wsdlDocumentUrl, Class portInterface, String endpointAddress) {
        JaxWsServiceServiceLocator<ServiceType> locator = new JaxWsServiceServiceLocator<ServiceType>();
        locator.setWsdlDocumentUrl(wsdlDocumentUrl);
        locator.setPortInterface(portInterface);
        locator.setEndpointAddress(endpointAddress);
        return withServiceLocator(locator);
    }

    public ServiceTemplateBuilder<ServiceType> withJaxWsServiceRefServiceLocator(Class portInterface, String endpointAddress) {
        JaxWsServiceRefServiceLocator<ServiceType> locator = new JaxWsServiceRefServiceLocator<ServiceType>();
        locator.setPortInterface(portInterface);
        locator.setEndpointAddress(endpointAddress);
        return withServiceLocator(locator);
    }

    public ServiceTemplateBuilder<ServiceType> withRemoteSlsbServiceLocator(Class<?> expectedType, String jndiName, Properties environment) {
        return withRemoteSlsbServiceLocator(new RemoteSlsbServiceLocator<ServiceType>(environment), expectedType, jndiName, false);
    }

    public ServiceTemplateBuilder<ServiceType> withRemoteSlsbServiceLocator(Class<?> expectedType, String jndiName, boolean resourceRef) {
        return withRemoteSlsbServiceLocator(new RemoteSlsbServiceLocator<ServiceType>(), expectedType, jndiName, resourceRef);
    }

    public ServiceTemplateBuilder<ServiceType> withLocalSlsbServiceLocator(Class<?> expectedType, String jndiName) {
        LocalSlsbServiceLocator<ServiceType> locator = new LocalSlsbServiceLocator<ServiceType>();
        locator.setExpectedType(expectedType);
        locator.setJndiName(jndiName);
        locator.setResourceRef(true);
        return withServiceLocator(locator);
    }

    public ServiceTemplateBuilder<ServiceType> withServiceHolder(ServiceType service) {
        return withServiceLocator(new ServiceHolder<ServiceType>(service));
    }

    public ServiceTemplateBuilder<ServiceType> withServiceLocator(ServiceLocator<ServiceType> locator) {
        serviceLocators.add(locator);
        return this;
    }

    public ServiceTemplate<ServiceType> build() {
        ServiceTemplate<ServiceType> template = new ServiceTemplate<ServiceType>();
        template.setTypeToLocate(serviceTypeClass);
        template.setServiceLocator(new ServiceLocatorChain<ServiceType>(serviceLocators));
        return template;
    }

    private ServiceTemplateBuilder<ServiceType> withRemoteSlsbServiceLocator(RemoteSlsbServiceLocator<ServiceType> locator, Class<?> expectedType, String jndiName, boolean resourceRef) {
        locator.setExpectedType(expectedType);
        locator.setJndiName(jndiName);
        locator.setResourceRef(resourceRef);
        locator.setHomeInterface(expectedType);
        return withServiceLocator(locator);
    }

    private ServiceTemplateBuilder<ServiceType> withJaxRpcServiceFactoryServiceLocator(JaxRpcServiceFactoryServiceLocator<ServiceType> locator, URL wsdlDocumentUrl, Class portInterface, String endpointAddress) {
        locator.setWsdlDocumentUrl(wsdlDocumentUrl);
        locator.setPortInterface(portInterface);
        locator.setEndpointAddress(endpointAddress);
        return withServiceLocator(locator);
    }
}
